//
//  SearchViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/19.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchCollectionViewCell.h"
#import "TAMacro.h"
#import "CollectionReusableView.h"

#define kCOllectionViewCellReuse @"reuse"
#define kCOllectionViewHeaderReuse @"headerResue"
@interface SearchViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate, UISearchControllerDelegate, UISearchResultsUpdating, UISearchDisplayDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *searchCollectionVIew;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *searchFlowLayout;

// mySearchController
@property (nonatomic, retain) UISearchController *mySearchController;
// 存放所有数据的数组
@property (nonatomic, retain) NSMutableArray *allDataArray;
// 存放搜索出结果的数组
@property (nonatomic, retain) NSMutableArray *searchResultDataArray;
// 搜索控制器
@property (nonatomic, retain) UISearchController *searchController;
// 搜索使用的表示图控制器
@property (nonatomic, retain) UITableViewController *searchTVC;
// searchBar
@property (nonatomic, retain) UISearchBar *bar;
// 搜索框输入的东西
@property (nonatomic, copy) NSString *inputText;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.searchCollectionVIew registerNib:[UINib nibWithNibName:@"SearchCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCOllectionViewCellReuse];
    [self.searchCollectionVIew registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCOllectionViewHeaderReuse];
    self.searchFlowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
    self.searchFlowLayout.minimumLineSpacing = 5;
    self.searchFlowLayout.minimumInteritemSpacing = 0;
    [self crateSearchbar];
}

- (void)crateSearchbar {
    self.bar = [[UISearchBar alloc] init];
    _bar.placeholder = @"请输入活动名称";
//    searchBar.backgroundColor = [UIColor whiteColor];
    _bar.frame = CGRectMake(0, 0, 200, 30);
    self.navigationItem.titleView = _bar;
    // 设置键盘类型
    self.bar.keyboardType = UIKeyboardTypeNamePhonePad;
    // searchBar  代理,记得签协议
    self.bar.delegate = self;

}

// searchBar的代理方法,当searchBar的textField开始编辑的时候调用,包含空
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    // 创建出搜索使用的表示图控制器
    self.searchTVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    _searchTVC.tableView.dataSource = self;
    _searchTVC.tableView.delegate = self;
    
    
    // 使用表示图控制器创建出搜索控制器
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:_searchTVC];
    
    // 搜索框检测代理
    //（这个需要遵守的协议是 <UISearchResultsUpdating> ，这个协议中只有一个方法，当搜索框中的值发生变化的时候，代理方法就会被调用）
    _searchController.searchResultsUpdater = self;
    _searchController.delegate = self;
    _searchController.searchBar.placeholder = @"搜索类型";
    //  _searchController.searchBar.delegate = self;
    [self presentViewController:_searchController animated:YES completion:^{
        // 当模态推出这个searchController的时候,需要把之前的searchBar隐藏,如果希望搜索的时候看不到热门搜索什么的,可以把这个页面给隐藏
        self.bar.hidden = YES;
        self.view.hidden = YES;
        
    }];
}

// 监听输入的关键字, 把输入的关键字,传到接口处去请求数据
#pragma mark - UISearchResultsUpdating Method
#pragma mark 监听者搜索框中的值的变化
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    // 1. 获取输入的值
    self.inputText = searchController.searchBar.text;
    
}

//4. 请求数据并解析(我这里用的时AFN请求, 其他方法都可以,随意)
// afnq请求数据(搜索数据)
- (void)searchContent {
    
    
}

// 设置搜索tableView cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_searchResultDataArray.count == 0) {
        NSLog(@"只有一行");
        return 1;
    }
    return _searchResultDataArray.count;
}

// 设置搜索tableView的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"indenfy";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    if (self.searchResultDataArray.count != 0) {
      
    }else{
        cell.textLabel.text = @"没有查找的内容";
        cell.imageView.image = nil;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

-(void)didDismissSearchController:(UISearchController *)searchController{
    self.bar.hidden = NO;
    self.view.hidden = NO;
    [self.searchController presentedViewController];

}
- (void)viewWillAppear:(BOOL)animated
{
    self.view.hidden = NO;
    
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.bar.hidden = NO;
    self.view.hidden = NO;
    [self.searchController presentedViewController];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 8;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCOllectionViewCellReuse forIndexPath:indexPath];
    
    cell.label.text = @"演唱会";
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (SCREEN_WIDTH - 20) / 4.0;
    
    return CGSizeMake(width, 20);
}
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.section == 0) {
        CollectionReusableView *reuseable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCOllectionViewHeaderReuse forIndexPath:indexPath];
        reuseable.titleLabel.text = @"一周关键词";
        return reuseable;

    } else if (indexPath.section == 1) {
        CollectionReusableView *reuseable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCOllectionViewHeaderReuse forIndexPath:indexPath];
        reuseable.titleLabel.text = @"精选关键词";
        return reuseable;

    } else {
        CollectionReusableView *reuseable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCOllectionViewHeaderReuse forIndexPath:indexPath];
        reuseable.titleLabel.text = @"搜索记录";
        return reuseable;
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, 44);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
